package com.springboot.huli.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.huli.entity.Paiban;
import com.springboot.huli.entity.Tiaoban;
import com.springboot.huli.entity.vo.PaibanVo;
import com.springboot.huli.entity.vo.TiaobanVo;
import com.springboot.huli.mapper.TiaobanMapper;
import com.springboot.huli.qpage.QPage;
import com.springboot.huli.service.IPaibanService;
import com.springboot.huli.service.ITiaobanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.huli.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 调班 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-29
 */
@Service
public class TiaobanServiceImpl extends ServiceImpl<TiaobanMapper, Tiaoban> implements ITiaobanService {

    @Autowired
    private IPaibanService iPaibanService;

    @Override
    public Result updates(Tiaoban obj) {
        this.updateById(obj);
        if(obj.getStatus()==2){//审核通过
            //更新原排班时间
            Paiban paiban=this.iPaibanService.getById(obj.getPbid());
            paiban.setUtime(new Date());
            paiban.setPbdate(obj.getNpbdate());
            paiban.setPbtype(obj.getNpbtype());
            this.iPaibanService.updateById(paiban);
        }
        return Result.reOk("审核成功");
    }

    @Override
    public Result insert(Tiaoban obj) {
        Paiban paiban=this.iPaibanService.getById(obj.getPbid());
        if(paiban!=null){
            obj.setYpbdate(paiban.getPbdate());
            obj.setYpbtype(paiban.getPbtype());
        }
        obj.setCtime(new Date());
        obj.setStatus(1);
        this.save(obj);
        return Result.reOk("新增调班成功");
    }

    @Override
    public IPage getPageList(QPage qPage, TiaobanVo obj) {
        IPage iPage = new Page(qPage.getPage(), qPage.getRows());
        List<TiaobanVo> mapList = this.baseMapper.getPageList(iPage,obj);
        return iPage.setRecords(mapList);
    }
}
